﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Phone7ActionPack.Emulation
{
    /// <summary>
    /// note that these are not yet implimented yet. setting this to anything wont change what it does.
    /// </summary>
    public enum EmulatorMovementSpeedRange
    {
        /// <summary>
        /// will try to go about 3MPH
        /// </summary>
        Walking,
        /// <summary>
        /// will try to go about 65MPH
        /// </summary>
        Driving,
        /// <summary>
        /// will try to go round about 400MPH
        /// </summary>
        Flying,
        /// <summary>
        /// totally random could show up anywhere
        /// </summary>
        SuperMan
    }
    public delegate void LocationWalkerChangedHandler(object sender,LocationWalkerChangedEventArgs e);
    public class LocationWalker
    {
        public static event LocationWalkerChangedHandler LocationWalkerChanged;
        private static Random r;
        static System.Windows.Threading.DispatcherTimer timer;
        // lets start out at some place nice
        private static double currentLatitude = (47.627453 * 1000000);
        private static double currentLongitude = (-122.242354 * 1000000);
        private static int minStep = 0;
        private static int maxStep = 50;
        private static int secondsBetweenReading = 3;
        private static System.Device.Location.GeoCoordinate lastLocation;
        private static System.Device.Location.GeoCoordinate currentLocation;
        private static void updateLocation()
        {
            currentLatitude = getNextValue((int)currentLatitude);
            currentLongitude = getNextValue((int)currentLongitude);
            currentLocation = new System.Device.Location.GeoCoordinate(currentLatitude / (double)1000000, currentLongitude / (double)1000000);

            if (LocationWalkerChanged != null)
                LocationWalkerChanged(null, new LocationWalkerChangedEventArgs() { Latitude = currentLocation.Latitude, Longitude = currentLocation.Longitude, ReadingDateTime = DateTime.Now, GeoCoordinate = currentLocation });
        }
        private static int getNextValue(int input)
        {
            // here is where we would factor in speed;
            return r.Next(input - maxStep, input + maxStep);
        }
        

        private static void init()
        {
            r = new Random();
            timer = new System.Windows.Threading.DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, secondsBetweenReading);
            timer.Tick += new EventHandler(timer_Tick);
            if (timer == null)
                throw new Exception("No timer");
        }

        static void timer_Tick(object sender, EventArgs e)
        {
            updateLocation();
        }

        public static void Enable()
        {
            if (timer == null)
                init();
            timer.Start();
        }
        public static void Disable()
        {
            if (timer == null)
                return;
            timer.Stop();
        }
        
    }
    public class LocationWalkerChangedEventArgs : EventArgs
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public DateTime ReadingDateTime { get; set; }
        public double Course { get; set; }
        public double Speed { get; set; }
        public System.Device.Location.GeoCoordinate GeoCoordinate { get; set; }
    }
}
